import {Body, Controller, Get, HttpStatus, Post, Query, Req, Res} from '@nestjs/common';
import {Response} from 'express';
import {CommonRedisService} from '../../service/common.redis.service';
import {CommonResponseDto} from '../../dto/common.response.dto';
import {ApplyStatus, ApproveStatus, ResCode} from '../../common/constants';
import AppConfig from 'config/app.config';
import {CommonRequestService} from "../../service/common.request.service";
import {uuid} from "../../common/utils";
import {getAccessToken, getDingDingHeaders} from "../../common/common";

const msgApi = '/message/workNotification'


@Controller('ding-msg')
export class MessageController{
    constructor(private readonly commonRedisService: CommonRedisService, private readonly commonRequestService: CommonRequestService) {

    }

    async sendApproveMsg(accountId: string, msg: string[], jumpStatus: ApproveStatus | ApplyStatus) {
        const token = await getAccessToken(this.commonRedisService, this.commonRequestService)
        console.log('token', token)
        const jumpUrl = "taurus://taurusclient/action/open_app?appId=0240812172047825&type=2&offline=false&page=%2Fpages%2Fapprove%2Fapprove&query=tab%3D"+jumpStatus
        const query = {
            organizationCodes: '',
            receiverIds: accountId,
            tenantId: 50805445,
            bizMsgId: uuid(32),
            msg: {
                "msgtype": "link",
                "link": {
                    "messageUrl": jumpUrl,
                    "picUrl":"@lALOACZwe2Rk",
                    "title": "审批通知",
                    "text": msg.join('；')
                }
            },
            // msg: {
            //     "msgtype": "action_card",
            //     "action_card": {
            //         "title": "审批通知",
            //         "markdown": msg.join('\n'),
            //         "statusInfo": {
            //             "text": "待审批",
            //             "bgcolor": "#FF9F00",
            //             "allcolor": "rgba(31, 31, 31)"
            //         },
            //         "btn_orientation": "1",
            //         "btn_json_list": [
            //             {
            //                 "title": "拒绝",
            //                 "action_pc_url": jumpUrl,
            //                 "action_mobile_url": jumpUrl
            //             },
            //             {
            //                 "title": "同意",
            //                 "action_pc_url": jumpUrl,
            //                 "action_mobile_url": jumpUrl
            //             }
            //         ]
            //     }
            // }
        }
        const headers = getDingDingHeaders('get', msgApi, query)
        try {
            const result = await this.commonRequestService.get(`${AppConfig.DingDing.Domain}${msgApi}`, {
                headers,
                params: query
            })
            if (result.success) {
                return new CommonResponseDto(ResCode.OK, '成功', result)
            }
            return new CommonResponseDto(ResCode.ERROR, '失败', result)
        } catch(e){
            console.log(e)
            return new CommonResponseDto(ResCode.ERROR, '服务异常', null)
        }
    }

    @Post('/apply')
    async sendApplyMsg(@Res({ passthrough: true }) res: Response) {
        res.status(HttpStatus.OK);
        const token = await getAccessToken(this.commonRedisService, this.commonRequestService)
        console.log('token', token)
        const api = '/message/workNotification'
        const query = {
            organizationCodes: '',
            receiverIds: 882840,
            tenantId: 50805445,
            bizMsgId: uuid(32),
            msg: {
                "msgtype": "link",
                "link": {
                    "messageUrl": "taurus://taurusclient/action/open_app?appId=0240812172047825&type=2&offline=false&page=%2Fpages%2Fmy%2Fmy",
                    "picUrl":"@lALOACZwe2Rk",
                    "title": "测试",
                    "text": "测试"
                }
            },
            // msg: {
            //     "msgtype": "action_card",
            //     "action_card": {
            //         "title": "审批通知",
            //         "markdown": "周强申请[Adobe Illustrator for Mac]收费软件的使用权限\n发起人:周强\n发起时间:2020-04-29 18:14:41",
            //         "statusInfo": {
            //             "text": "待审批",
            //             "bgcolor": "#FF9F00",
            //             "allcolor": "rgba(31, 31, 31)"
            //         },
            //         "btn_orientation": "1",
            //         "btn_json_list": [
            //             {
            //                 "title": "拒绝",
            //                 "action_pc_url": "taurus://taurusclient/action/open_app?appId=0240812172047825&type=2&offline=false&page=%2Fpages%2Findex%2Findex",
            //                 "action_mobile_url": "taurus://taurusclient/action/open_app?appId=0240812172047825&type=2&offline=false&page=%2Fpages%2Findex%2Findex"
            //             },
            //             {
            //                 "title": "同意",
            //                 "action_pc_url": "taurus://taurusclient/action/open_app?appId=0240812172047825&type=2&offline=false",
            //                 "action_mobile_url": "taurus://taurusclient/action/open_app?appId=0240812172047825&type=2&offline=false"
            //             }
            //         ]
            //     }
            // }
        }
        const headers = getDingDingHeaders('get', api, query)
        try {
            const result = await this.commonRequestService.get(`${AppConfig.DingDing.Domain}${api}`, {
                headers,
                params: query
            })
            if (result.success) {
                return new CommonResponseDto(ResCode.OK, '成功', result)
            }
            return new CommonResponseDto(ResCode.ERROR, '失败', result)
        } catch(e){
            console.log(e)
            return new CommonResponseDto(ResCode.ERROR, '服务异常', null)
        }

    }
}
